# Encoding: UTF-8
# 手写 Makefile
# Author: dousha99
# Date: 2020-03

## 此 Makefile 已经弃用，转用 cmake
## 不过你仍然可以用 make 搞出东西

# Variables
TARGET=arm-none-eabi
LINK_SCRIPT=STM32F407VET6_FLASH.ld
PROJECT=skylab

# Files
INC=inc/ driver/inc/
C_SRC=$(wildcard src/*.c driver/src/*.c) $(wildcard src/*/*.c) $(wildcard src/*/*/*.c)
S_SRC=$(wildcard src/*.s) $(wildcard src/*/*.s) $(wildcard src/*/*/*.s)
C_OBJ=$(C_SRC:.c=.o)
S_OBJ=$(S_SRC:.s=.o)
OBJ=$(C_OBJ) $(S_OBJ)
OCD_SCRIPT=STM32F407.cfg

# Toolchain
CC=${TARGET}-gcc
CXX=${TARGET}-g++
AR=${TARGET}-ar
ASM=${TARGET}-gcc
OBJCOPY=${TARGET}-objcopy
OBJDUMP=${TARGET}-objdump
SIZE=${TARGET}-size
DBG=${TARGET}-gdb
LD=${TARGET}-ld
OCD=openocd

# Build flags (don't touch)
DEFINITIONS=-DSTM32F40_41xxx -D"assert_param(expr) ((void) 0);"
COMMON_FLAGS=-std=gnu11 -mcpu=cortex-m4 -mtune=cortex-m4 -mfpu=auto -mthumb -mthumb-interwork -ffunction-sections -fdata-sections -fno-common -fmessage-length=0 -specs=nano.specs -fno-asynchronous-unwind-tables
FPU_FLAGS=-mfloat-abi=hard -mfpu=fpv4-sp-d16
OPTIMIZATION=-g
INC_FLAGS=-Iinc/ -Idriver/inc/
LIB_FLAGS=-Llib/ -larm_cortexM4lf_math
BASIC_FLAGS=${DEFINITIONS} ${COMMON_FLAGS} ${FPU_FLAGS} ${INC_FLAGS} ${LIB_FLAGS} ${OPTIMIZATION}
C_FLAGS=${BASIC_FLAGS} -std=gnu11
CXX_FLAGS=${BASIC_FLAGS} -std=c++11
LINK_FLAGS=${BASIC_FLAGS} -Wl,-gc-sections,--print-memory-usage -T ${LINK_SCRIPT} --verbose -Xlinker -Map=${PROJECT}.map

# Build rules
${PROJECT}.bin : ${PROJECT}.elf
	${OBJCOPY} -Obinary $< $@

${PROJECT}.elf : ${OBJ}
	${CC} ${LINK_FLAGS} -o $@ ${OBJ}

%.o : %.s
	${ASM} ${COMMON_FLAGS} -c -o $@ $<

%.o : %.c
	${CC} ${C_FLAGS} -c -o $@ $<

%.o : %.cpp
	${CXX} ${CXX_FLAGS} -c -o $@ $<

clean:
	rm -rf ${OBJ}

flash: ${PROJECT}.bin
	${OCD} -f ${OCD_SCRIPT}

ldiag: ${OBJ}
	${CC} ${LINK_DIAG_FLAGS} -o ${PROJECT}.elf ${OBJ}

doc: Doxyfile
	doxygen
